//
//  AVCaptureDevice+DL.m
//  DLVideoCapture_Example
//
//  Created by Duanhu on 2025/2/25.
//  Copyright © 2025 dbh. All rights reserved.
//

#import "AVCaptureDevice+DL.h"

@implementation AVCaptureDevice (DL)
/// 从当前手机寻找符合需要的采集设备。
+ (AVCaptureDevice *)cameraWithPosition:(AVCaptureDevicePosition)position {
    NSArray<AVCaptureDeviceType> *deviceTypes = @[AVCaptureDeviceTypeBuiltInWideAngleCamera];//设备类型：广角镜头、双镜头
    if (@available(iOS 10.2, *)) {
        deviceTypes = @[AVCaptureDeviceTypeBuiltInWideAngleCamera, AVCaptureDeviceTypeBuiltInDualCamera];
    }
    
    if (@available(iOS 13.0, *)) {
        // 数组成员的顺序，决定搜索设备的顺序，不要随意调整。
        deviceTypes = @[AVCaptureDeviceTypeBuiltInTripleCamera,AVCaptureDeviceTypeBuiltInDualWideCamera,AVCaptureDeviceTypeBuiltInWideAngleCamera, AVCaptureDeviceTypeBuiltInDualCamera];
    }
        
    AVCaptureDeviceDiscoverySession *deviceDiscoverySession = [AVCaptureDeviceDiscoverySession  discoverySessionWithDeviceTypes:deviceTypes mediaType:AVMediaTypeVideo position:position];
    NSArray *devices = deviceDiscoverySession.devices;
    
    for (AVCaptureDevice *device in devices) {
        if ([device position] == position) {
            return device;
        }
    }
    return nil;
}

/// 配置对焦、曝光、白平衡等
- (void)defaultConfiguration {
    if ([self isSmoothAutoFocusSupported]) {
        self.smoothAutoFocusEnabled = YES;
    }
    if ([self isFocusModeSupported:AVCaptureFocusModeContinuousAutoFocus]) {
        self.focusMode = AVCaptureFocusModeContinuousAutoFocus;
    }
    if ([self isExposureModeSupported:AVCaptureExposureModeContinuousAutoExposure]) {
        self.exposureMode = AVCaptureExposureModeContinuousAutoExposure;
    }
    if ([self isWhiteBalanceModeSupported:AVCaptureWhiteBalanceModeContinuousAutoWhiteBalance]) {
        self.whiteBalanceMode = AVCaptureWhiteBalanceModeContinuousAutoWhiteBalance;
    }
}

@end
